package zm.irc.ioc.pojo;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.mysql.cj.util.StringUtils;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.yaml.snakeyaml.Yaml;


public class ConfigurationCache {
    private static final Logger log = Logger.getLogger(ConfigurationCache.class);
    private static final ConcurrentHashMap<String,Map<String,Object>> cfgCache = new ConcurrentHashMap<>();


    private ConfigurationCache(){}

    public static void loadLocalCfg(String path){
        if(StringUtils.isNullOrEmpty(path)){
            return;
        }
        if(exists(path)){
            return;
        }
        try{
            log.info("weechat config file : " + path);
            File configFile = new File( path);
            InputStream file = new FileInputStream(configFile);
            Map<String,Object> conf = new Yaml().load(file);
            cfgCache.put(path, conf);
        }catch(Exception e){
            log.error("Init local cfg error!",e);
        }
    }

    public static void loadCfg(String fileName){
        log.error(" load cfg ： " + fileName);
        if(exists(fileName)){
            return;
        }
        try{
            InputStream file = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            Map<String,Object> conf = new Yaml().load(file);
            cfgCache.put(fileName, conf);
        }catch(Exception e){
            log.error("Load CFG file error!",e);
        }
        
    }

    public static boolean exists(String fileName){
        return cfgCache.contains(fileName);
    }


    public static <T>T getValue(String fileName,String valuePath,Class<T> c){
        return (T)getValue(fileName,valuePath);
    }

    public static Object getValue(String fileName,String valuePath){
        if(StringUtils.isEmptyOrWhitespaceOnly(fileName) || StringUtils.isEmptyOrWhitespaceOnly(valuePath)){
            return null;
        }
        Map<String,Object> fileContent = cfgCache.get(fileName);
        if( fileContent == null){
            return null;
        }

        String[] paths = valuePath.split("\\.");
        LinkedList<String> pathList = toList(paths);

        return doGetValue(fileContent,pathList);
    }


    private static LinkedList<String> toList(String[] arr){
        LinkedList<String> pathList = new LinkedList<>();
        if(arr == null || arr.length == 0){
            return pathList;
        }
        for(String a : arr){
            pathList.add(a);
        }

        return pathList;

    }

    private static Object doGetValue(Map<String,Object> map,LinkedList<String> path){
        String currentNodeName = path.pollFirst();
        if(CollectionUtils.isEmpty(path)){
            return map.get(currentNodeName);
        }

        return doGetValue((Map<String,Object>)map.get(currentNodeName),path);
    }
    public static void clean(){
        cfgCache.clear();
    }
}
